package com.wyh.contorller;

import com.wyh.entity.Houses;
import com.wyh.entity.Houses_Id_Address;
import com.wyh.entity.RestBean;
import com.wyh.entity.User_house;
import com.wyh.mapper.HouseMapper;
import com.wyh.mapper.UserMapper;
import com.wyh.mapper.User_HouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Validated
@RestController
@RequestMapping("/api/house")
public class HouseController {

    @Resource
    HouseMapper houseMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private User_HouseMapper user_HouseMapper;

    //通过用户uid查找房屋
    @GetMapping("/find")
    public RestBean<List<Houses>> selectHouseByUserid(@RequestParam("uid") int uid) {
        List<Houses> houses = houseMapper.findHousesByUserId(uid);
        if (houses != null && !houses.isEmpty()) {
            return RestBean.success(houses); // 假设RestBean.success接受一个List<House>
        }
        return RestBean.failure(400); // 提供一个更具体的错误消息
    }

    //获取所有的房屋
    @GetMapping("/getAll")
    public RestBean<List<Houses>> getAll() {
        List<Houses> houses = houseMapper.findAllHouse();
        if (houses != null && !houses.isEmpty()) {
            return RestBean.success(houses);
        }
        return RestBean.failure(400);
    }

    @GetMapping("/getAllHouseIDAndAddress")
    public RestBean<List<Houses_Id_Address>> getAllHouseIDAndAddress() {
        List<Houses_Id_Address> houses = houseMapper.getAllHouseIDAndAddress();
        if (houses != null && !houses.isEmpty()) {
            return RestBean.success(houses);
        }
        return RestBean.failure(400);
    }

    //添加房屋
    @PostMapping("/add")
    public RestBean<String> add(Houses houses) {
        Houses existinghouse = houseMapper.findExist(houses.getHouse_id());

        if (existinghouse == null) {
            try {
                houses.setAddress(houses.getAddress());
                houses.setState(houses.getState());
                houses.setRemark(houses.getRemark());
                houseMapper.addHouse(houses);

                User_house user_house = new User_house();
                user_house.setHouse_id(houseMapper.findHouse_idByAddress(houses.getAddress()));
                user_house.setUser_id(userMapper.findUidByUsername(houses.getUsername()));

                user_HouseMapper.insertUser_House(user_house);
                return RestBean.success("房屋信息添加成功！");

            } catch (Exception e) {
                e.printStackTrace();
                return RestBean.failure(500, "添加房屋时发生错误：" + e.getMessage());
            }
        }
        return RestBean.failure(400, "该房屋已存在");
    }

    //修改房屋信息
    // todo bug: api/house/getAllHouse没有返回uid,我没法传uid,方法失效了
    @PutMapping("/update")
    public RestBean<String> update(@RequestBody Houses home) {
        Houses existingHouse = houseMapper.findExist(Long.parseLong(String.valueOf(home.getHouse_id())));
        if (existingHouse != null) {
            try {
                home.setAddress(StringUtils.hasText(home.getAddress()) ? home.getAddress() : existingHouse.getAddress());
                home.setRemark(StringUtils.hasText(home.getRemark()) ? home.getRemark() : existingHouse.getRemark());
                home.setState(StringUtils.hasText(home.getState()) ? home.getState() : existingHouse.getState());
                houseMapper.updateHouse(home);
                return RestBean.success("房屋信息修改成功！");
            } catch (Exception e) {
                e.printStackTrace();
                return RestBean.failure(500, "修改房屋信息时发生错误：" + e.getMessage());
            }
        }
        return RestBean.failure(400, "该房屋不存在");
    }

    //删除房屋信息
    @DeleteMapping("/delete")
    public RestBean<String> delete(long house_id) {
        Houses existingHouse = houseMapper.findExist(house_id);
        if (existingHouse != null) {
            try {
                houseMapper.deleteHouse(house_id);
                return RestBean.success("房屋信息删除成功！");
            } catch (Exception e) {
                e.printStackTrace();
                return RestBean.failure(500, "删除房屋信息时发生错误：" + e.getMessage());
            }
        }
        return RestBean.failure(400, "该房屋不存在");
    }
}
